package qc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.quickconnect.ControlObject;
import org.quickconnect.QuickConnect;

import ui.MainFrame;

import com.mysql.jdbc.Connection;

import controller.ServerConnectionHandler;

public class CheckIfAlreadyModeratorValCO implements ControlObject{

	@Override
	public Object handleIt(ArrayList<Object> arg0) {
		HashMap map = (HashMap) arg0.get(arg0.size()-1);
		String username = (String) map.get("userName");
		String session = (String) map.get("sessionId");
		Connection con = (Connection)MainFrame.mainFrame.getController().getConnectionPool().getConnection();
		java.sql.PreparedStatement select = null;
		ResultSet results = null;
		try {
				select = con.prepareStatement("SELECT * FROM Moderator WHERE UserID = (SELECT UserID FROM User WHERE UserName = ?) AND SessionID = (SELECT SessionID FROM Session WHERE SessionNumber = ?)");
				select.setString(1, username);
				select.setString(2, session);
				results = select.executeQuery();
				if (!results.next())
				{
					MainFrame.mainFrame.getController().getConnectionPool().returnConnection(con);
					return true; //Is not a moderator
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return false; //Is a moderator
	}
}